package com.zhida.jiyangcenter.coupon

import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import chooongg.base.annotation.ContentLayout
import chooongg.base.annotation.TitleBar
import chooongg.base.utils.resColor
import chooongg.base.utils.startActivity
import chooongg.base.utils.tipFail
import chooongg.base.widget.recyclerView.divider.HorizontalDividerItemDecoration
import chooongg.http.APIException
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.zhida.jiyangcenter.R
import com.zhida.jiyangcenter.api.APICoupon
import com.zhida.jiyangcenter.api.BaseAPI
import com.zhida.jiyangcenter.api.CouponListEntity
import com.zhida.jiyangcenter.mian.BaseActivity
import com.zhida.jiyangcenter.modle.HealthyTestStroeActivity
import com.zhida.jiyangcenter.modle.ShopDetailsActivity
import com.zhida.jiyangcenter.utils.glideImage
import com.zhida.jiyangcenter.utils.request
import kotlinx.android.synthetic.main.activity_my_coupon.*

@TitleBar(false)
@ContentLayout(R.layout.activity_my_coupon)
class MyCouponActivity : BaseActivity() {
    val adapter = Adapter()
    override fun initConfig(savedInstanceState: Bundle?) {
        title_bar.navigationIcon(R.drawable.iv_back_fill) {
            finish()
        }.title("优惠券")
        refresh_layout.setOnRefreshListener {
            getData()
        }
        rv_my_coupon.layoutManager = LinearLayoutManager(context)
        rv_my_coupon.addItemDecoration(
            HorizontalDividerItemDecoration.Builder(context).sizeResId(R.dimen.dividerBig)
                .colorResId(R.color.colorBack).build()
        )
        rv_my_coupon.adapter = adapter
        adapter.setOnItemClickListener { _, view, position ->
            if (adapter.data[position].applicable_type == "1") {
                startActivity<HealthyTestStroeActivity> {
                    putExtra("uuid", adapter.data[position].shop_id)
                    putExtra("type", "1")
                }
            } else {
                startActivity<ShopDetailsActivity> {
                    putExtra(
                        "uuid",
                        adapter.data[position].shop_id
                    )
                }

            }
        }
        btn_my_coupon_history.setOnClickListener {
            startActivity<CouponHistoryActivity> { }
        }
        btn_my_coupon_collection.setOnClickListener {
            startActivity<CouponCollectionActivity> { }
        }
        tv_all.setOnClickListener {
            tv_all.setTextColor(resColor(R.color.colorPrimary))
            tv_shop.setTextColor(resColor(R.color.textColor))
            tv_product.setTextColor(resColor(R.color.textColor))
            type = "all"
            getData()
        }
        tv_shop.setOnClickListener {
            tv_shop.setTextColor(resColor(R.color.colorPrimary))
            tv_all.setTextColor(resColor(R.color.textColor))
            tv_product.setTextColor(resColor(R.color.textColor))
            type = "shop"
            getData()
        }
        tv_product.setOnClickListener {
            tv_product.setTextColor(resColor(R.color.colorPrimary))
            tv_all.setTextColor(resColor(R.color.textColor))
            tv_shop.setTextColor(resColor(R.color.textColor))
            type = "product"
            getData()
        }
        getData()
    }

    var type = "all"
    private fun getData() {
        showTipLoading()
        lifecycleScope.request(
            { APICoupon.service().userCoupon(type) },
            object : BaseAPI<List<CouponListEntity>>() {
                override fun onSuccess(t: List<CouponListEntity>?, state: Int, msg: String) {
                    hideTipLoading()
                    refresh_layout.finishRefresh()
                    adapter.setNewData(t)
                }

                override fun onError(e: APIException) {
                    hideTipLoading()
                    refresh_layout.finishRefresh()
                    tipFail(e.eMessage)
                }

            })
    }

    class Adapter : BaseQuickAdapter<CouponListEntity, BaseViewHolder>(R.layout.item_my_coupon) {
        override fun convert(helper: BaseViewHolder, item: CouponListEntity?) {
            glideImage(helper.getView(R.id.iv_my_coupon_cover), item?.coupon_img ?: "")
            helper.setText(R.id.tv_my_coupon_name, item?.shop_product_name ?: "中昊云景")
                .setText(
                    R.id.tv_my_coupon_money,
                    "￥${if (item?.classification == "1") (item.classification_b.toString() + item?.strategy) else (item?.demomination.toString() + "立减券")}"
                )
                .setText(R.id.tv_coupon_type, item?.coupon_name)
                .setText(R.id.tv_coupon_time, item?.start_time + "-" + item?.end_time)
                .setGone(R.id.tv_my_coupon_jindian, item?.applicable_type == "1")
        }

    }
}